/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.um.sso.othersystem;

import com.jinhe.tss.core.Global;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.exception.UserIdentificationException;
import com.jinhe.tss.core.sso.IOperator;
import com.jinhe.tss.core.sso.IPWDOperator;
import com.jinhe.tss.core.sso.context.Context;
import com.jinhe.tss.core.sso.context.RequestContext;
import com.jinhe.tss.core.sso.identifier.BaseUserIdentifier;
import com.jinhe.tss.core.util.InfoEncoder;
import com.jinhe.tss.um.service.ILoginService;

/**
 * <p>
 *  Token身份认证器 <br>
 *  通过验证是否存在Token来判断用户是否已经登录 一期的平台，如果是，则让其在新平台登录。 <br>
 *  
 *  用户在一期的平台中登录以后，通过以下地址转入到门户中： <br>
 *  http://ip/pms/login.do?identifier=com.jinhe.tss.um.identification.identifier.PTTokenIdentifier&sso=true <br>
 *  需要在PMS的application.properties文件中设置SSO成功后调整的页面地址，例如： <br>
 *  sso.index.page = /tss/default.portal <br>
 *  默认login.do只返回成功信息，但如果有sso=true和sso.index.page的配置同时存在，则会自动sendRedirect至sso.index.page页面。
 * </p>
 */
public class PTTokenIdentifier extends BaseUserIdentifier {
    
	private static InfoEncoder encrypt = new InfoEncoder(); 
    
    ILoginService service = (ILoginService) Global.getContext().getBean("LoginService");
 
    protected IOperator validate() throws UserIdentificationException {
        RequestContext requestContext = Context.getRequestContext();

        String token = requestContext.getValueFromRequest("token");
        if(token == null) throw new UserIdentificationException("Token为空，用户可能还没有登录，请重新登录");
   
        String orignToken = encrypt.createDecryptor(token); //解码
        String loginName = orignToken.split(",")[3];
        
        IPWDOperator operator = service.getOperatorDTOByLoginName(loginName);
        
        if (operator == null) throw new BusinessException("用户在UMS里不存在！");
        
        return operator;
    }
}
